﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HRWEB.Models
{
    public class DeptEmployeeModel
    {
        public List<TEmployee> Employees { get; set; }
        public string SelectedDeptCode { get; set; }
        public string SelectedDeptName { get { return Depts.Where(x => x.FDeptCode == SelectedDeptCode).Single().FName; } }
        public List<TDictDept> Depts { get; set; }
        public bool DispAll { get; set; }
        public string DispAllButtonText { get { return DispAll ? "切换显示全部/未成功确认（当前显示全部）" : "切换显示全部/未成功确认（当前显示未成功确认）"; } }
        public void BuildFromDeptCode(string deptCode)
        {
            Build(deptCode, false);
        }
        public void BuildFromKQDeptCode(string kqDeptCode)
        {
            Build(kqDeptCode, true);
        }
        private void Build(string code, bool isKQ)
        {
            hrdbEntities ent = new hrdbEntities();
            List<TEmployee> list = new List<TEmployee>();
            this.SelectedDeptCode = code;
            IQueryable<TDept> s;
            if (isKQ)
            {
                s = ent.TDepts.Where(p => p.FKQDeptCode == this.SelectedDeptCode);
            }
            else
            {
                s = ent.TDepts.Where(p => p.FDeptCode == this.SelectedDeptCode);
            }
            var s2 = s.Where(p => p.FInvalidDate == null);
            s2 = s2.Where(p => p.TEmployee.TEmployeeStatus.OrderByDescending(q => q.FOperateDate).FirstOrDefault().FStatusCode == "01");
            List<string> ids = new List<string>();
            foreach (TDept dept in s2)
            {
                //TEmployee emp = dept.TEmployee;
                //list.Add(emp);
                ids.Add(dept.FEmployeeID);                                
            }
            var kkk = ent.TEmployees.Where(p => ids.Contains(p.FEmployeeID));
            this.Employees = kkk.ToList();
        }
    }
}